package com.twu.biblioteca.service;

import com.twu.biblioteca.dao.ItemDao;
import com.twu.biblioteca.exception.CheckoutFailedException;
import com.twu.biblioteca.exception.ReturnFailedException;
import com.twu.biblioteca.model.Item;

import java.io.PrintStream;
import java.util.List;
import java.util.Scanner;

public class ItemService {
    private static final String PROMPT_INPUT_RETURN_INDEX = "Index To Return (0 to abort): ";
    private static final String MESSAGE_RETURN_SUCCESS = "Return Success!\n";
    private static final String MESSAGE_RETURN_FAILED = "Return Failed!\n";
    private static final String PROMPT_INPUT_CHECKOUT_INDEX = "Index To Checkout (0 to abort): ";
    private static final String MESSAGE_CHECKOUT_SUCCESS = "Checkout Success!\n";
    private static final String MESSAGE_CHECKOUT_FAILED = "Checkout Failed!\n";

    private ItemDao itemDao;
    private PrintStream printStream = System.out;
    private Scanner scanner = new Scanner(System.in);

    public ItemService(ItemDao itemDao) {
        this.itemDao = itemDao;
        itemDao.prepareData();
    }

    public void handleCheckoutItem() {
        printStream.print(PROMPT_INPUT_CHECKOUT_INDEX);
        int input = scanner.nextInt();
        if (input == 0) return;
        try {
            checkoutItem(input - 1);
            printStream.print(MESSAGE_CHECKOUT_SUCCESS);
        } catch (CheckoutFailedException exception) {
            printStream.print(MESSAGE_CHECKOUT_FAILED);
        }
    }

    public void handleReturnItem() {
        printStream.print(PROMPT_INPUT_RETURN_INDEX);
        int input = scanner.nextInt();
        if (input == 0) return;
        try {
            returnItem(input - 1);
            printStream.print(MESSAGE_RETURN_SUCCESS);
        } catch (ReturnFailedException exception) {
            printStream.print(MESSAGE_RETURN_FAILED);
        }
    }

    public void printUnavailableItemList() {
        printItemLists(itemDao.getUnavailableItemLists());
    }

    public void printAvailableItemList() {
        printItemLists(itemDao.getAvailableItemLists());
    }

    private void printItemLists(List<Item> itemList) {
        int index = 0;
        for (Item item : itemList) {
            index++;
            printStream.println(String.valueOf(index) + ". " + item.toString());
        }
    }

    private void checkoutItem(int index) throws CheckoutFailedException {
        if (itemDao.getAvailableItemLists().size() > index) {
            itemDao.checkoutItem(itemDao.getAvailableItemLists().get(index));
            return;
        }
        throw new CheckoutFailedException();
    }

    private void returnItem(int index) throws ReturnFailedException {
        if (itemDao.getUnavailableItemLists().size() > index) {
            itemDao.returnItem(itemDao.getUnavailableItemLists().get(index));
            return;
        }
        throw new ReturnFailedException();
    }

    public void setItemDao(ItemDao itemDao) {
        this.itemDao = itemDao;
    }

    public void setPrintStream(PrintStream printStream) {
        this.printStream = printStream;
    }

    public void setScanner(Scanner scanner) {
        this.scanner = scanner;
    }


}
